package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.service.IOrderUrgeMsgService;
import com.mdd.admin.validate.OrderUrgeMsgCreateValidate;
import com.mdd.admin.validate.OrderUrgeMsgUpdateValidate;
import com.mdd.admin.validate.OrderUrgeMsgSearchValidate;
import com.mdd.admin.vo.OrderUrgeMsgListedVo;
import com.mdd.admin.vo.OrderUrgeMsgDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.OrderUrgeMsg;
import com.mdd.common.mapper.OrderUrgeMsgMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 订单催发货消息实现类
 * @author DEREK
 */
@Service
public class OrderUrgeMsgServiceImpl implements IOrderUrgeMsgService {
        
    @Resource
    OrderUrgeMsgMapper orderUrgeMsgMapper;

    /**
     * 订单催发货消息列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<OrderUrgeMsgListedVo>
     */
    @Override
    public PageResult<OrderUrgeMsgListedVo> list(PageValidate pageValidate, OrderUrgeMsgSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<OrderUrgeMsg> queryWrapper = new QueryWrapper<>();
        
        Integer shopId = LikeAdminThreadLocal.getShopId();
        if(null != shopId && shopId > 1) {
        	queryWrapper.eq("shop_id", shopId);
        }
        queryWrapper.orderByDesc("id");

        orderUrgeMsgMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:userId@user_id:int",
            "=:orderSn@order_sn:str",
            "=:orderStatus@order_status:int",
        });

        IPage<OrderUrgeMsg> iPage = orderUrgeMsgMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<OrderUrgeMsgListedVo> list = new LinkedList<>();
        for(OrderUrgeMsg item : iPage.getRecords()) {
            OrderUrgeMsgListedVo vo = new OrderUrgeMsgListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 订单催发货消息详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return OrderUrgeMsg
     */
    @Override
    public OrderUrgeMsgDetailVo detail(Integer id) {
        OrderUrgeMsg model = orderUrgeMsgMapper.selectOne(
                new QueryWrapper<OrderUrgeMsg>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        OrderUrgeMsgDetailVo vo = new OrderUrgeMsgDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 订单催发货消息新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(OrderUrgeMsgCreateValidate createValidate) {
        OrderUrgeMsg model = new OrderUrgeMsg();
        model.setUserId(createValidate.getUserId());
        model.setOrderSn(createValidate.getOrderSn());
        model.setOrderId(createValidate.getOrderId());
        model.setOrderStatus(createValidate.getOrderStatus());
        model.setRemark(createValidate.getRemark());
        model.setExtend(createValidate.getExtend());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        orderUrgeMsgMapper.insert(model);
    }

    /**
     * 订单催发货消息编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(OrderUrgeMsgUpdateValidate updateValidate) {
        OrderUrgeMsg model = orderUrgeMsgMapper.selectOne(
                new QueryWrapper<OrderUrgeMsg>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUserId(updateValidate.getUserId());
        model.setOrderSn(updateValidate.getOrderSn());
        model.setOrderId(updateValidate.getOrderId());
        model.setOrderStatus(updateValidate.getOrderStatus());
        model.setRemark(updateValidate.getRemark());
        model.setExtend(updateValidate.getExtend());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        orderUrgeMsgMapper.updateById(model);
    }

    /**
     * 订单催发货消息删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        OrderUrgeMsg model = orderUrgeMsgMapper.selectOne(
                new QueryWrapper<OrderUrgeMsg>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        orderUrgeMsgMapper.delete(new QueryWrapper<OrderUrgeMsg>().eq("id", id));
    }

}
